package libs

import (
	"encoding/json"
	"fmt"
	"reflect"
	"strconv"
	"strings"
)

// Int2String int转字符串
func Int2String(val int) string {
	return strconv.Itoa(val)
}

// Int64String int64 转字符串
func Int64String(val int64) string {
	return strconv.FormatInt(val, 10)
}

// String2Int 字符串转int
func String2Int(val string) int {
	num, _ := strconv.Atoi(val)
	return num
}

// String64Int 字符串转int64
func String64Int(val string) int64 {
	num, _ := strconv.ParseInt(val, 10, 64)
	return num
}

// Float2Int 浮点型转换成int
func Float2Int(val float64) int {
	num, _ := strconv.Atoi(fmt.Sprintf("%1.0f", val))
	return num
}

// Float64String float64转字符串,per控制精度(四舍五入)
func Float64String(val float64, pre int) string {
	return strconv.FormatFloat(val, 'f', pre, 64)
}

// Interface2String Interface转成字符串
func Interface2String(val interface{}) (string, bool) {
	var res string
	var has bool = true
	switch val.(type) {
	case int:
		res = strconv.FormatInt(int64(val.(int)), 10)
	case int8:
		res = strconv.FormatInt(int64(val.(int8)), 10)
	case int16:
		res = strconv.FormatInt(int64(val.(int16)), 10)
	case int32:
		res = strconv.FormatInt(int64(val.(int32)), 10)
	case int64:
		res = strconv.FormatInt(val.(int64), 10)
	case uint:
		res = strconv.FormatUint(uint64(val.(uint)), 10)
	case uint8:
		res = strconv.FormatUint(uint64(val.(uint8)), 10)
	case uint16:
		res = strconv.FormatUint(uint64(val.(uint16)), 10)
	case uint32:
		res = strconv.FormatUint(uint64(val.(uint32)), 10)
	case uint64:
		res = strconv.FormatUint(val.(uint64), 10)
	case float32:
		res = strconv.FormatFloat(float64(val.(float32)), 'f', -1, 32)
	case float64:
		res = strconv.FormatFloat(val.(float64), 'f', -1, 64)
	case string:
		res = val.(string)
	default:
		res = ""
		has = false
	}
	return res, has
}

// JsonEncode  序列化
func JsonEncode(data interface{}) []byte {
	json, err := json.Marshal(data)
	if err != nil {
		return nil
	}
	return json
}

// JsonDecode 反序列化
func JsonDecode(content []byte, data interface{}) error {
	err := json.Unmarshal(content, data)
	return err
}

// JsonBytes map转换成[]byte切片
func JsonBytes(data interface{}) (bty []byte, err error) {
	json, err := json.Marshal(data)

	if err != nil {
		return bty, err
	}

	bty = []byte(string(json))
	return bty, err
}

// HttpBindQuery 拼接字符串(中文保持原状)
func HttpBindQuery(params map[string]string) (query string) {
	paramsSlice := make([]string, 0, 0)
	for k, v := range params {
		s := fmt.Sprintf("%v=%v", k, v)
		paramsSlice = append(paramsSlice, s)
	}

	query = strings.Join(paramsSlice, "&")
	return
}

// String2IntSlice []string转换成[]int64
func String2IntSlice(data []string) []int64 {
	res := make([]int64, 0)
	for _, s := range data {
		res = append(res, String64Int(s))
	}
	return res
}

// StructToMap 结构体转map
func StructToMap(s interface{}) map[string]interface{} {
	val := reflect.ValueOf(s).Elem() // 取得结构体指针的元素值
	typ := val.Type()                // 取得结构体类型
	m := make(map[string]interface{})
	for i := 0; i < typ.NumField(); i++ {
		f := val.Field(i)
		key := typ.Field(i).Name
		m[key] = f.Interface()
	}
	return m
}

// ArrayColumn 切边升维(类比php的array_column功能)
func ArrayColumn(list []map[string]interface{}, field string) map[string]map[string]interface{} {
	data := make(map[string]map[string]interface{}, len(list))

	for _, item := range list {
		data[item[field].(string)] = item
	}
	return data
}
